package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsIssue;
import com.qf.service.DtsIssueService;
import com.qf.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 13:19
 */
@RestController
@RequestMapping("/admin/issue")
@Slf4j
@CrossOrigin
//T http://localhost:8080/admin/issue/list?page=1&limit=20&question=ss&sort=add_time&order=des
public class IssueController {
    @Autowired
    private DtsIssueService dtsIssueService;
    @RequiresPermissionsDesc(menu = {"商场管理","通用问题"},button ="查找")
    @RequiresPermissions("admin:issue:list")
    @GetMapping("/list")
    public Object issueList(Integer page, Integer limit, String sort, String order, String question) {
        Page<DtsIssue> dtsIssuePage = dtsIssueService.selectpage(page, limit, sort, order, question);
        List<DtsIssue> records = dtsIssuePage.getRecords();
        long total = dtsIssuePage.getTotal();
        HashMap<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", records);
        return ResponseUtil.ok(data);

    }
    @RequiresPermissionsDesc(menu = {"商场管理","通用问题"},button ="添加")
    @RequiresPermissions("admin:issue:create")
    @PostMapping("/create")
    public Object issueCreate(@RequestBody DtsIssue dtsIssue) {
        if(dtsIssue==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsIssue.setAddTime(new Date());
        dtsIssue.setUpdateTime(new Date());
        dtsIssue.setDeleted(false);
        dtsIssueService.insert(dtsIssue);
        return ResponseUtil.ok(dtsIssue);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","通用问题"},button ="编辑")
    @RequiresPermissions("admin:issue:update")
    @PostMapping("/update")
    public Object issueUpdate(@RequestBody DtsIssue dtsIssue) {
        if(dtsIssue==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsIssue.setUpdateTime(new Date());
        dtsIssueService.update(dtsIssue);
        return ResponseUtil.ok(dtsIssue);
    }
    @RequiresPermissionsDesc(menu = {"商场管理","通用问题"},button ="删除")
    @RequiresPermissions("admin:issue:delete")
    @PostMapping("/delete")
    public Object issueDelete(@RequestBody DtsIssue dtsIssue) {
        if(dtsIssue==null){
            return ResponseUtil.badArgumentValue();
        }
        dtsIssue.setDeleted(true);
        dtsIssueService.delete(dtsIssue);
        return ResponseUtil.ok();
    }

    // http://localhost:8080/admin/keyword/list?page=1&limit=20&keyword=ee&url=eee&sort=add_time&order=desc
}
